﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nhCommon
{
    public class NhDb
    {
        /// <summary>
        /// 程序的值转为数据库值，如为null，转为DBNull
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static object ValueToDb<T>(T t)
        {
            if (t != null)
            {
                return t;
            }
            return DBNull.Value;
        }

        /// <summary>
        /// 数据库值转为程序值，如有DBnull转为默认值，null或0等
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="o"></param>
        /// <returns></returns>
        public static T DbToValue<T>(Object o)
        {
            if (o != DBNull.Value && o != null)
            {
                return (T)o;  //非空类型可能返回空
            }
            return default;
        }
    }
}
